package com.isias.system.service.Impl;

import com.isias.model.dto.CourseDto;
import com.isias.model.dto.CourseWithStar;
import com.isias.model.dto.PingLunDto;
import com.isias.model.dto.UserAndIdDto;
import com.isias.model.vo.CourseVo;
import com.isias.model.vo.SelectCourseVo;
import com.isias.model.vo.Star;
import com.isias.system.mapper.CourseDtoMapper;
import com.isias.system.mapper.SysUserMapper;
import com.isias.system.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDtoMapper courseDtoMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public List<CourseDto> SelectAll(SelectCourseVo courseVo) {
        return courseDtoMapper.SelectAll(courseVo);
    }

    @Override
    public CourseDto SelectOne(String id) {
        return courseDtoMapper.SelectOne(id);
    }

    @Override
    public Boolean UpdataOneById(CourseVo courseVo) {
        return courseDtoMapper.UpdataOneById(courseVo) == 1;
    }

    @Override
    public Boolean RemoveOneById(String id) {
        return courseDtoMapper.RemoveOneById(id) == 1;
    }

    @Override
    public Boolean Add(CourseVo courseVo) {
        return courseDtoMapper.Add(courseVo) == 1;
    }

    @Override
    public Boolean RemoveByIds(List<String> ids) {
        return courseDtoMapper.RemoveByIds(ids) > 0;
    }

    @Override
    public List<UserAndIdDto> SelectUserAndId() {
        List<UserAndIdDto> list = sysUserMapper.SelectUserAndId();
        return list;
    }

    @Override
    public Boolean UpdateUrlById(String url, String id) {
        return courseDtoMapper.UpdateUrlById(url, id) > 0;
    }

    @Override
    public List<CourseDto> SelectAllById(String Id, SelectCourseVo courseVo) {
        return courseDtoMapper.SelectAllById(Id,courseVo);
    }

    @Override
    public List<CourseDto> SelectMyCourse(String id, SelectCourseVo courseVo) {
        return courseDtoMapper.SelectMyCourse(id, courseVo);
    }

    @Override
    public Boolean AddCourseToMe(String userid, String Courseid) {
        String judgement = courseDtoMapper.Judgement(userid, Courseid);
        if (judgement==null||"".equals(judgement)){
            return courseDtoMapper.AddCourseToMe(userid,Courseid) > 0;
        }

        return false;
    }

    @Override
    public Boolean DeleteCourseToMe(String userid, String Courseid) {
        return courseDtoMapper.DeleteCourseToMe(userid,Courseid) > 0;
    }

    @Override
    public Boolean Star(String id, String courseId, Star star) {
        return courseDtoMapper.StarAndDescription(id,courseId,star) > 0;
    }

    @Override
    public Double GetStar(String courseId) {
        Double star = (double) courseDtoMapper.GetStar(courseId);
        Double num = (double) courseDtoMapper.GetStarNum(courseId);
        return star/num;
    }

    @Override
    public Star GetStarInfo(String getId, String courseId) {
        return courseDtoMapper.GetStarInfo(getId,courseId);
    }

    @Override
    public List<CourseWithStar> SelectAllStar() {
        return courseDtoMapper.SelectAllStar();
    }

    @Override
    public List<PingLunDto> GetPingLun(String courseId) {
        return courseDtoMapper.GetPingLun(courseId);
    }

    @Override
    public CourseWithStar GetStarCourse() {
        List<CourseWithStar> courseWithStars = courseDtoMapper.SelectAllStar();
        return courseWithStars.get(0);
    }

    @Override
    public CourseWithStar GetMostStudy() {
        List<CourseWithStar> courseWithStars = courseDtoMapper.SelectMostStudy();
        return courseWithStars.get(0);
    }


}
